﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UsageMetering.Contract;
using UsageMetering.Service;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace UsageMetering.Host
{
    class Program
    {
        static void Main(string[] args)
        {
            //HostServiceViaCode();
            HostSerivceViaConfiguration();
        }

        static void HostServiceViaCode()
        {
            Uri httpBaseAddress = new Uri("http://localhost:8888/UsageMetering");
            Uri tcpBaseAddress = new Uri("net.tcp://localhost:9999/UsageMetering");

            using (ServiceHost UsageMeteringSerivceHost = new ServiceHost(typeof(UsageMeteringService), httpBaseAddress, tcpBaseAddress))
            {
                WSHttpBinding httpBinding = new WSHttpBinding();
                NetTcpBinding tcpBinding = new NetTcpBinding();

                UsageMeteringSerivceHost.AddServiceEndpoint(typeof(IUsageMeteringService), httpBinding, string.Empty);
                UsageMeteringSerivceHost.AddServiceEndpoint(typeof(IUsageMeteringService), tcpBinding, string.Empty);

                ServiceMetadataBehavior behavior = UsageMeteringSerivceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
                {
                    if (behavior == null)
                    {
                        behavior = new ServiceMetadataBehavior();
                        behavior.HttpGetEnabled = true;
                        UsageMeteringSerivceHost.Description.Behaviors.Add(behavior);
                    }
                    else
                    {
                        behavior.HttpGetEnabled = true;
                    }
                }

                UsageMeteringSerivceHost.Opened += delegate
                {
                    Console.WriteLine("Service via code started!");
                };

                UsageMeteringSerivceHost.Open();

                Console.Read();
            }
        }

        static void HostSerivceViaConfiguration()
        {
            using (ServiceHost UsageMeteringSerivceHost = new ServiceHost(typeof(UsageMeteringService)))
            {
                UsageMeteringSerivceHost.Opened += delegate
                {
                    Console.WriteLine("Service via conf started!");
                };

                UsageMeteringSerivceHost.Open();

                Console.Read();
            }
        }        
    }
}
